/**
 * 许可证添加接口测试用例
 *
 * Created by snowingsea on 2020/02/25.
 */
const fs = require('fs');
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const path = require('path');

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');

const { statusKind } = require('./../../../../phoenix/models/license');
const utility = require('./../../../../phoenix/models/utility');

module.exports = describe('/POST license.json', function () {
  const url = '/license.json';
  const administrator = new Administrator();
  const statusKinds = [
    statusKind.normal,
    statusKind.illegal,
    statusKind.notMatch,
    statusKind.exceedTime,
    statusKind.exceedTester,
    statusKind.exceedTime + statusKind.exceedTester,
  ];

  common.should.be.user(uri, url, 'post');
  common.should.be.administrator(uri, url, 'post');

  // 使用文件正常添加许可证
  statusKinds.forEach((status, index) => {
    it(`it should ${index === 0 ? 'ADD' : 'add'} a license of ${status} with file successfully`, function (done) {
      const licensePath = path.join(__dirname, `../../../benchmark/license/${status}`);
      administrator.login()
        .then(() => {
          chai.request(uri)
            .post(url)
            .set('Authorization', `Bearer ${administrator.token.accessToken}`)
            .attach('file', fs.readFileSync(licensePath), status.toString())
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.licenseMessage(res.body);
              res.body.status.should.be.eql(status);
              done();
            });
        })
        .catch(done);
    });
  });

  // 使用许可证字符串正常添加许可证
  statusKinds.forEach((status) => {
    it(`it should add a license of ${status} with license string successfully`, function (done) {
      const licensePath = path.join(__dirname, `../../../benchmark/license/${status}`);
      administrator.login()
        .then(() => utility.readFile(licensePath))
        .then((license) => {
          chai.request(uri)
            .post(url)
            .set('Authorization', `Bearer ${administrator.token.accessToken}`)
            .query({ license })
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.licenseMessage(res.body);
              res.body.status.should.be.eql(status);
              done();
            });
        })
        .catch(done);
    });
  });

  // 使用空许可证文件
  it('it should fail to add a license of nothing', function (done) {
    administrator.login()
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .query({ license: '' })
          .end((err, res) => {
            res.should.have.status(400);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 使用错误许可证文件
  it('it should fail to add a license of something wrong', function (done) {
    const licensePath = path.join(__dirname, '../../../benchmark/license/wrong');
    administrator.login()
      .then(() => utility.readFile(licensePath))
      .then((license) => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .query({ license })
          .end((err, res) => {
            res.should.have.status(200);
            res.body.status.should.be.eql(statusKind.illegal);
            done();
          });
      })
      .catch(done);
  });
});
